﻿(function ($, W) {
    dialog.defaults.okValue = "确定";
    dialog.defaults.title = "提示";
    dialog.defaults.ok = $.noop;
    var Withdrawal =
        function () {
            var isPercentCharge = $("#isPercentCharge").length > 0;
            var encashmentCharge = $("#encashmentCharge").text();
            var maxAmount = $("#maxAmount").length > 0 ? ($("#maxAmount").text() - 0): 0;
            var Model = {};
            Model.submitting = false;
            var flag;
            //初始化
            Model.init = function () {
                $(document).on('click.dialogAuditData', '#dialogAuditData', function (evt) {
                    evt.preventDefault();
                    ////amount, cardId, pwd
                    var param = [$.trim($("#aumount").val()) - 0, $('#bankCardList .on').attr("data-id") - 0, $.trim($("#pwd").val())];
                    utils.cookie.set('withdrawalParams', param.join('|'), 1, '/');
                    window.location.href = U('member/cash/flow');	//	有效流水
                })
                //输入验证
                $("#aumount").on("keyup blur", function (evt) {
                    var value = $(this).val().replace(/[^0-9.]/g, "");
                    var strA = value.split(".");
                    if (strA.length > 1) {
                        if (strA[0].length == 0) strA[0] = "0"
                        value = strA[0] + "." + strA[1].substr(0, 2);
                    }
                    $(this).val(value);
                    if (isPercentCharge) {
                        var amount = value * encashmentCharge * 0.01;
                        if (maxAmount > 0 && maxAmount < amount) {
                            amount = maxAmount;
                        }
                        amount = amount.toFixed(4) - 0;
                        $('#deduction').text(amount);
                        $('#afterAmount').val(value - amount);
                    }
                    else {
                        $('#afterAmount').val(value - $('#deduction').val());
                    }
                });
                $("#bankCardList a.bank-box").click(function (evt) {
                    if (!$(this).hasClass('on')) {
                        $("#bankCardList a.bank-box").removeClass('on');
                        $(this).addClass('on');
                    }
                })
                $("#button").click(Model.sbmForm);
                Model.balance();
                Model.MemberCards();
            };
            //会员可用银行卡的列表
            Model.cardList = [];
            //提交表单
            Model.sbmForm = function () {
                var that = $(this);
                $("#button").prop("disabled", true);
                if (flag) { return false; }
                flag = true;
                setTimeout(function () { flag = false; }, 1000);//1秒内禁止二次点击
                var max = $("#encashmentmax").text() - 0;
                var min = $("#encashmentmin").text() - 0;
                var balance = $("#balanceLb").text() - 0;

                var reg = /^\d*(.\d{0,2})?$/;
                var param = {
                    money: $.trim($("#aumount").val()) - 0,
                    cardid: $('#bankCardList .on').attr("data-id") - 0,//$.trim($("#cardId").val()) - 0,
                    pwd: $.trim($("#pwd").val())
                };

                if (!reg.test("" + param.money)) {
                    dialog("提款金额输入不能超过两位小数").showModal();
                    $("#button").prop("disabled", false);
                    return false;
                }
                else if (balance < param.money) {
                    dialog("提款金额超出当前余额").showModal();
                    $("#button").prop("disabled", false);
                    return false;
                }
                else if (min > param.money) {
                    dialog("最小取款金额" + min + "元").showModal();
                    $("#button").prop("disabled", false);
                    return false;
                }
                else if (max > 0 && param.money > max) {
                    dialog("最大取款金额" + max + "元").showModal();
                    $("#button").prop("disabled", false);
                    return false;
                }
                else if ($.trim($("#pwd").val()) == "") {
                    dialog("请填写取款密码").showModal();
                    $("#button").prop("disabled", false);
                    return false;
                }
                var deduction = Number($("#deduction").text());
                var feiyong = deduction + window['totalDeductionRebate'] + window['totalDeductionAdmincharge'];
                //if (param.money <= feiyong) {
                //    dialog("您输入的提款金额小于需扣除的总费用, 无法提款!").showModal();
                //    return;
                //}
                var cont = '';
                //if ((window['totalDeductionRebate'] > 0 || window['totalDeductionAdmincharge'] > 0) && ) {
                if (deduction > 0) {
                    cont += '<p>需要扣除手续费: <span style="color:red">' + deduction + '</span></p>'
                }
                if (window['totalDeductionRebate'] > 0) {
                    cont += '<p>未通过优惠流水审核！需要扣除优惠: <span style="color:red">' + window['totalDeductionRebate'] + '</span></p>'
                }
                if (window['totalDeductionAdmincharge']) {
                    cont += '<p>未通过常态性流水审核！需要扣除行政费: <span style="color:red">' + window['totalDeductionAdmincharge'] + '</span></p>'
                }
                if (!cont) {
                    next();
                }
                else {
                    var content = '<h3><a id="dialogAuditData" href="javascript:;">查看流水</a></h3>';
                    content += cont + '是否继续出款?';
                    dialog({
                        content: content,
                        ok: function () { next(); },
                        cancel: function () { $("#button").prop("disabled", false); },
                        skin: 'dialog-custom withdrawal-dialog',
                        okValue: "坚持出款",
                        button: [
                            {
                                value: '继续投注',
                                callback: function () {
                                    var w = window.parent || window;
                                    w.location.href = './';
                                }
                            },
                        ]
                    }).showModal();
                }
                function next() {
                    if (Model.submitting) return;
                    Model.submitting = true;
                    var dia = dialog("出款申请提交中, 请稍候.....").show();
                    $.ajax({
                        url: U('member/cash'),
                        type: "post",
                        data: param,
						dataType: 'json',
                        success: function (data) {
                            dia.close().remove();
                            if (data.status) {
                                //dialog("取款信息已提交，请等待处理").showModal();
                                setTimeout(function () {
                                    alert("取款信息已提交，请等待处理");
                                    that.removeAttr("disabled");
                                    window.location.reload();
                                }, 10);
                            }
                            else {
                                $("#button").prop("disabled", false);
                                dialog("申请提交失败！" + data.msg).showModal();
                            }
                        },
                        complete: function () {
                            Model.submitting = false;
                        }
                    });
                }
            }
            //显示会员余额
            Model.balance = function () {
                $.getJSON(U('member/info/api') + '&t=' + new Date().getTime(), function (data) {
                    $("#balanceLb").text(data.CreditBalance);
                    $("#totalBanlance").text(data.CreditBalance);
                })
            }
            //会员收款银行卡列表初始化
            Model.MemberCards = function () {
                var optionStr = "";
                for (var i = 0; i < Model.cardList.length; i++) {
                    var item = Model.cardList[i];
                    optionStr += '<option value="' + item.FID + '" >' + item.FBankName + '--' + item.FCardNo + '</option>';
                }
                $("#cardId").html(optionStr);
                Model.cardSelect();
            }
            //银行卡选中
            Model.cardSelect = function () {
                var index = $("option:selected", "#cardId").index();
                var selectItem = Model.cardList[index];
                if (selectItem != undefined) {
                    $("#lbHolder").text(selectItem.FHolders);
                    $("#lbCardNO").text(selectItem.FCardNo);
                    $("#lbBranch").text(selectItem.FBranch);
                }
            }
            //重置
            Model.reset = function () {
                $("#aumount,#pwd").val('');
            }
            return Model;
        }
    W.Withdrawal = Withdrawal();
})(jQuery, window)